package com.hymjweb.demo.hymj.log_manage.data_column.mapper;

import com.hymjweb.demo.framework.cache.bean.AMyCacheTreeNode;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author DaTing created on 2020-09-17
 **/
@Mapper
@Component
public interface DataColumnMapper {

    class DataColumnProvider{

        public String translateColumnName(String nameColumnName, String tableName,String idColumnName,String idValue) {
            return new SQL() {{
                SELECT(nameColumnName);
                FROM(tableName);
                WHERE(idColumnName + "=" + DataTypeUtil.quoteStr(idValue));
            }}.toString();
        }

        public String getOnePojoFromDb(String tableName,String idValue) {
            String sql = new SQL() {{
                SELECT("*");
                FROM(tableName);
                if(idValue.indexOf("'") == -1){
                    WHERE( " id = " + DataTypeUtil.quoteStr(idValue));
                }
                else{
                    WHERE( " id in (" + idValue + ") ");
                }
            }}.toString();
            return sql;
        }

    }

    @SelectProvider(type = DataColumnProvider.class, method = "translateColumnName")
    String translateColumnName(@Param("nameColumnName") String nameColumnName, @Param("tableName") String tableName,@Param("idColumnName") String idColumnName,@Param("idValue") String idValue);

    @Select("select upper(concat_ws('.',table_name,column_name)) as id, column_comment as name, 0 as pid from information_schema.columns where table_schema = 'hymj'")
    List<AMyCacheTreeNode> user_col_comments();

    @SelectProvider(type = DataColumnProvider.class, method = "getOnePojoFromDb")
    Map<String,Object> getOnePojoFromDb(@Param("tableName") String tableName, @Param("idValue") String idValue);

    @SelectProvider(type = DataColumnProvider.class, method = "getOnePojoFromDb")
    List<Map<String,Object>> queryByIdsFromDb(@Param("tableName") String tableName, @Param("idValue") String idValue);

}
